package cn.cxj.order.service.impl;

import cn.cxj.basic.constant.OrderTypeConstants;
import cn.cxj.basic.service.impl.BaseServiceImpl;
import cn.cxj.basic.util.CodeGenerateUtils;
import cn.cxj.basic.util.QuartzUtils;
import cn.cxj.order.domain.AdoptOrder;
import cn.cxj.order.domain.OrderAddress;
import cn.cxj.order.mapper.AdoptOrderMapper;
import cn.cxj.order.mapper.OrderAddressMapper;
import cn.cxj.order.service.IAdoptOrderService;
import cn.cxj.pay.domain.AlipayInfo;
import cn.cxj.pay.domain.PayBill;
import cn.cxj.pay.mapper.AlipayInfoMapper;
import cn.cxj.pay.mapper.PayBillMapper;
import cn.cxj.pay.service.IPayService;
import cn.cxj.pet.domain.Pet;
import cn.cxj.pet.mapper.PetMapper;
import cn.cxj.quartz.domain.QuartzJobInfo;
import cn.cxj.quartz.jobs.MainJob;
import cn.cxj.quartz.service.IQuartzService;
import cn.cxj.user.domain.Logininfo;
import cn.cxj.user.domain.User;
import cn.cxj.user.domain.UserAddress;
import cn.cxj.user.mapper.UserAddressMapper;
import cn.cxj.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private AdoptOrderMapper adoptOrderMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private IPayService payService;

    @Autowired
    private IQuartzService quartzService;

    @Override
    @Transactional
    public String submit(Map<String, String> params, Logininfo logininfo) {
        // 获取前端传过来的数据
        String serviceMethod = params.get("service_method");
        Long addressId = Long.valueOf(params.get("address_id"));
        Integer payMethod = Integer.valueOf(params.get("pay_method"));
        Long petId = Long.valueOf(params.get("pet_id"));
        // 基于petId查询t_pet
        Pet pet = petMapper.loadById(petId);
        // 下架宠物
        pet.setState(0);
        // 设置宠物的领养人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        // 修改宠物信息
        petMapper.update(pet);
        // 基于addressId查询t_user_address表,获取到地址
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        // 维护t_order_adopt,保存并返回自增id
        AdoptOrder adoptOrder = initAdoptOrder(pet,user);
        adoptOrderMapper.save(adoptOrder);
        // 复制一份地址到t_order_address表,设置order_id,保存并返回自增id
        OrderAddress orderAddress = initOrderAddress(userAddress);
        orderAddress.setOrderSn(adoptOrder.getOrderSn());
        orderAddress.setOrder_id(adoptOrder.getId());
        orderAddressMapper.save(orderAddress);
        // 修改t_order_adopt的addressId
        adoptOrder.setAddress_id(orderAddress.getId());
        adoptOrderMapper.update(adoptOrder);

        // 生成一个支付订单
        PayBill payBill = initPayBill(adoptOrder, payMethod);
        // 保存到t_pay_bill表
        payBillMapper.save(payBill);

        // 根据商铺id查询到商铺的私钥,appid,支付宝公钥
        AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBill.getShop_id());
        // 调用支付
        String result = payService.pay(alipayInfo, payBill);

        // 添加一个定时器
        Map<String, Object> map = new HashMap<>();
        map.put("orderSn", payBill.getOrderSn());
        QuartzJobInfo quartzJobInfo = new QuartzJobInfo();
        quartzJobInfo.setType(MainJob.class);
        quartzJobInfo.setParams(map);
        quartzJobInfo.setFireDate(payBill.getLastPayTime());
        quartzJobInfo.setJobName(payBill.getOrderSn());
        quartzService.addJob(quartzJobInfo);

        return result;
    }

    /**
     * 初始化支付订单
     * @param adoptOrder
     * @param payMethod
     * @return
     */
    private PayBill initPayBill(AdoptOrder adoptOrder, Integer payMethod) {
        PayBill payBill = new PayBill();
        BeanUtils.copyProperties(adoptOrder, payBill);
        payBill.setMoney(adoptOrder.getPrice());
        payBill.setCreateTime(new Date());
        payBill.setPayChannel(payMethod);
        payBill.setBusinessType(OrderTypeConstants.ADOPT_ORDER);
        payBill.setBusinessKey(adoptOrder.getId());
        return payBill;
    }

    /**
     * 初始化订单地址
     * @param userAddress 用户地址
     * @return
     */
    private OrderAddress initOrderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        return orderAddress;
    }

    /**
     * 初始化领养订单
     * @param pet 宠物信息
     * @param user 用户信息
     * @return
     */
    private AdoptOrder initAdoptOrder(Pet pet, User user) {
        AdoptOrder adoptOrder = new AdoptOrder();
        adoptOrder.setDigest("[宠物之家]-领养订单");
        adoptOrder.setState(0);
        adoptOrder.setPrice(pet.getSaleprice());
        adoptOrder.setLastPayTime(new Date(System.currentTimeMillis()+1000*60));
        adoptOrder.setUser_id(user.getId());
        adoptOrder.setShop_id(pet.getShop_id());
        adoptOrder.setPet_id(pet.getId());
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        adoptOrder.setOrderSn(orderSn);
        return adoptOrder;
    }
}
